package com.ag.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Page;

/**
 * @author <a href="mailto:rplees.i.ly@gmail.com">rplees</a>
 * date 2010-03-29
 * {@code} 分页实体类
 * @param <T> 实体
 */
public class PageInfo<T> {
	public static final int DEFAULT_PAGE_SIZE = 20;
	
	int pageSize = DEFAULT_PAGE_SIZE;    				//每页显示数 
	int currentPage = 1;  				//当前页码，即要跳转的页码
	int totalCount = -1;   			//数据的总数（？条）
	int totalPage = -1;
	Collection<T> items ;				//查询返回的结果集
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public Collection<T> getItems() {
		return items;
	}

	public void setItems(Collection<T> items) {
		this.items = items;
	}

	public static <K> PageInfo<K> with(Page<K> sourcePage) {
		if(sourcePage == null) return null;
		
		PageInfo<K> page = new PageInfo<K>();
		page.setCurrentPage(NumberUtils.isNotValid(sourcePage.getNumber()) ? 1: sourcePage.getNumber() + 1);
		page.setItems(sourcePage.getContent());
		page.setPageSize(sourcePage.getSize());
		page.setTotalCount(new Long(sourcePage.getTotalElements()).intValue());
		page.setTotalPage(sourcePage.getTotalPages());
		return page;
	}
	
	public static <K> PageInfo<K> with(Collection<K> items, PageParam p, int count) {
		PageInfo<K> page = new PageInfo<K>();
		page.setItems(items);
		page.setCurrentPage(p.getPageNum());
		page.setPageSize(p.getPageSize());
		page.setTotalCount(count);
		
		int totalPage = ((count - 1) / p.getPageSize() ) + 1;
		page.setTotalPage(totalPage);
		return page;
	}
	
	public static <K> PageInfo<K> with(Collection<K> items, PageParam p, long c) {
		int count = new Long(c).intValue();
		PageInfo<K> page = new PageInfo<K>();
		page.setItems(items);
		page.setCurrentPage(p.getPageNum());
		page.setPageSize(p.getPageSize());
		page.setTotalCount(count);
		
		int totalPage = ((count - 1) / p.getPageSize() ) + 1;
		page.setTotalPage(totalPage);
		return page;
	}
	
	public static <K> PageInfo<K> with(List<K> list, PageParam p) {
		if(PageParam.checkInvalid(p)) {
			p = new PageParam();
		}
		
		List<K> content = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(list)) {
			int start = p.start() - 1; 
			int end = p.end() - 1;
			
			int pEnd = end;
			int idx = list.size() - 1;
			if(idx < start) {
				
			} else {
				if(pEnd > idx) {
					pEnd = idx;
				}
				content = list.subList(start, pEnd + 1);
			}
		}
		
		int count = list.size();
		
		PageInfo<K> page = new PageInfo<K>();
		page.setItems(content);
		page.setCurrentPage(p.getPageNum());
		page.setPageSize(p.getPageSize());
		page.setTotalCount(count);
		
		int totalPage = ((count - 1) / p.getPageSize() ) + 1;
		page.setTotalPage(totalPage);
		return page;
	}
	
	public static <K> PageInfo<K> with(Collection<K> items, Page<?> sourcePage) {
		if(sourcePage == null) return null;
		
		PageInfo<K> page = new PageInfo<K>();
		page.setCurrentPage(NumberUtils.isNotValid(sourcePage.getNumber()) ? 1: sourcePage.getNumber() + 1);
		page.setItems(items);
		page.setPageSize(sourcePage.getSize());
		page.setTotalCount(new Long(sourcePage.getTotalElements()).intValue());
		page.setTotalPage(sourcePage.getTotalPages());
		return page;
	}
}